#include "mycoin.h"
#include <QtDebug>
#include <QTimer>

MyCoin::MyCoin(const QString& butImg,QWidget* parent):QPushButton(parent){
    QPixmap pixmap;
    bool ret = pixmap.load(butImg);
    if(!ret){
        qDebug() << butImg << "加载图片失败!";
    }
    this->setFixedSize(pixmap.width(),pixmap.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    m_min = 1;
    m_max = 8;
    m_timer1 = new QTimer(this);
    m_timer2 = new QTimer(this);
    connect(m_timer1,&QTimer::timeout,this,&MyCoin::onTimer1Timeout);
    connect(m_timer2,&QTimer::timeout,this,&MyCoin::onTimer2Timeout);
    m_IsAnimation = false;
    m_isWin = false;
}

void MyCoin::changeFlag(){
    if(this->m_flag){
        m_timer1->start(30);
        this->m_IsAnimation = true;
        this->m_flag = false;
    }
    else{
        m_timer2->start(30);
        this->m_IsAnimation = true;
        this->m_flag = true;
    }
}

void MyCoin::onTimer1Timeout(){
    QPixmap pixmap;
    QString str = QString(":/res/Coin000%1.png").arg(this->m_min++);
    pixmap.load(str);
    this->setFixedSize(pixmap.width(),pixmap.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    if(this->m_min > this->m_max){
        this->m_min = 1;
        this->m_IsAnimation = false;
        m_timer1->stop();
    }
}

void MyCoin::onTimer2Timeout(){
    QPixmap pixmap;
    QString str = QString(":/res/Coin000%1.png").arg(this->m_max--);
    pixmap.load(str);
    this->setFixedSize(pixmap.width(),pixmap.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    if(this->m_max < this->m_min){
        this->m_max = 8;
        this->m_IsAnimation = false;
        m_timer2->stop();
    }
}

void MyCoin::mousePressEvent(QMouseEvent* ev){
    if(this->m_IsAnimation || this->m_isWin){
        return;
    }
    return QPushButton::mousePressEvent(ev);
}
